using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**蝙蝠 */
public class Bat : MonoBehaviour
{
    /**飞行速度 */
    public float speed = 6.0f;

    /**左对象 */
    public Transform leftObj;
    
    /**右对象 */
    public Transform rightObj;
    
    private PolygonCollider2D _polygonCollider2D;
    private BoxCollider2D _triggerCollider2D;
    private Animator _animator;
    
    /**初始位置 */
    private Vector3 _originalPosition;
    
    /**左坐标x */
    private float _leftX;
    
    /**右坐标x */
    private float _rightX;

    /**是否激活 */
    private bool _isActive = false;

    /**是否飞行中 */
    private bool _isFly = false;

    private static readonly int Active = Animator.StringToHash("active");
    private static readonly int Fly = Animator.StringToHash("fly");

    // Start is called before the first frame update
    void Start()
    {
        _polygonCollider2D = GetComponent<PolygonCollider2D>();
        _triggerCollider2D = GetComponent<BoxCollider2D>();
        _animator = GetComponent<Animator>();
        
        _originalPosition = transform.position;

        _leftX = leftObj.position.x;
        _rightX = rightObj.position.x;
    }

    // Update is called once per frame
    void Update()
    {
        if (!_isFly)
        {
            return;
        }

        var position = transform.position;
        position = new Vector3(position.x + speed * Time.deltaTime, Math.Max(position.y - Math.Abs(speed)* Time.deltaTime, _originalPosition.y - 1.0f), position.z);
        
        if (position.x < _leftX)
        {
            transform.position = new Vector3(_leftX, position.y, position.z);
            speed = Math.Abs(speed);
            
            var scale = transform.localScale;
            transform.localScale = new Vector3(-Math.Abs(scale.x), scale.y, scale.z);
        } 
        else if (position.x > _rightX)
        {
            transform.position = new Vector3(_rightX, position.y, position.z);
            speed = Math.Abs(speed) * -1;
            
            var scale = transform.localScale;
            transform.localScale = new Vector3(Math.Abs(scale.x), scale.y, scale.z);
        }
        else
        {
            transform.position = new Vector3(position.x, position.y, position.z);
        }
        
    }

    private void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.CompareTag("Player") && !_isActive)
        {
            _isActive = true;
            _animator.SetTrigger(Active);

            speed = col.transform.position.x < transform.position.x ? Math.Abs(speed) * -1 : Math.Abs(speed);

            var scale = transform.localScale;
            transform.localScale = new Vector3((speed > 0.0f ? -1.0f: 1.0f) * Math.Abs(scale.x), scale.y, scale.z);
            
            Invoke(nameof(ActiveFinish), 1.0f);
        }
    }

    /**激活完成 */
    private void ActiveFinish()
    {
        _animator.SetTrigger(Fly);
        Invoke(nameof(FlyReadyFinish), 0.35f);
    }

    /**飞行准备完成 */
    private void FlyReadyFinish()
    {
        _isFly = true;
    }
}
